<?php



/**
 * Skeleton subclass for representing a row from the 'kpi' table.
 *
 *
 *
 * You should add additional methods to this class to meet the
 * application requirements.  This class will only be generated as
 * long as it does not already exist in the output directory.
 *
 * @package    propel.generator.empeval
 */
class EmpevalKpi extends EmpevalBaseKpi
{
	
	const STATUS_ACTIVE = 1;
	const STATUS_CANCELED = 0;
	const STATUS_DELETED = -1;
	
	public function calculateResult() {
		if ($this->getWeight() == null || $this->getRealization80() == null || $this->getRealization100() == null)
			return null;
		if ($this->getIndicators()->getCode() == 'd') {
			$d80 = new DateTime($this->getRealization80());
			$d100 = new DateTime($this->getRealization100());
			$dra = new DateTime($this->getResultAchieved());
			$ra = $dra->getTimestamp();
			$r80 = $d80->getTimestamp();
			$r100 = $d100->getTimestamp();
		}
		else {
			$ra = (float)$this->getResultAchieved();
			$r80 = (float)$this->getRealization80();
			$r100 = (float)$this->getRealization100();
		}
		if ($this->getTrend() == 'higher') {
			if ($ra < $r80) {
				return 0;
			}
			else if ($ra >= $r100) {
				return 100;
			}
			return (($ra - $r80) / ($r100 - $r80)) * 20 + 80;
		}
		else {
			if ($ra > $r80) {
				return 0;
			}
			else if ($ra <= $r100) {
				return 100;
			}
			return ((-$ra + $r80) / (-$r100 + $r80)) * 20 + 80;
		}
	}
	
	public function calculateWeightResult() {
		if ($this->getResult() == null)
			return null;
		return $this->getResult() * ($this->getWeight() / 100);
	}
	
	public function setResultAchieved($v) {
		parent::setResultAchieved($v);
		$this->setResult($this->calculateResult());
		$this->setWeightResult($this->calculateWeightResult());
	}
	
}
